package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"mysql/dao"
	"mysql/model"
)

// database/sql -- jdbc 规范
// github.com/go-sql-driver/mysql -- mysql对应驱动程序
func main() {

	// 查询 QueryRow
	//user, err := dao.GetUserById(1)
	//if err != nil {
	//	log.Fatalln(err)
	//}
	//fmt.Println(user)
	//// Query
	//users, err := dao.GetUsers()
	//if err != nil {
	//	log.Fatalln(err)
	//}
	//fmt.Println(users)

	// 新增, 修改, 删除 Exec
	// execDML(err)

	//err := dao.Transfer("tom", "jerry", 200)
	//if err != nil {
	//	log.Fatalln(err)
	//}

	//err := dao.TransferWrapper("tom", "jerry", 200)
	//if err != nil {
	//	log.Fatalln(err)
	//}

	user, err := dao.GetUser(1)
	if err != nil {
		log.Fatalln(err)
	}

	users, err := dao.GetUsers2()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(user)
	fmt.Println(users)

	result, err := dao.InsertUserByNamedExec()
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("result = ", result)
}

func execDML(err error) {
	rows, err := dao.InsertUser(&model.User{Name: "Jack", Age: 20})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(rows)

	rows, err = dao.UpdateUser(&model.User{Id: 1, Name: "tomtomtom", Age: 100})
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(rows)

	rows, err = dao.DeleteUser(2)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(rows)
}
